char_div = document.createElement("div")
char_div.className = "char_helper"
char_div.id = "char"
char_div.innerHTML=`
<img class="char_img" id="char_img" src="">
<div class="char_helper_bar" id="char_msg_bar">
    <div class="char_say_bar" id="char_msg"></div>
</div>`
document.body.appendChild(char_div)
window.addEventListener('pywebviewready',async function() {
    window.charData=await window.pywebview.api.get_charData()
})
function change_msg(cid,msg_obj){
    msg_bar = document.getElementById("char_msg_bar")
    char_img = document.getElementById("char_img")
    try{
        document.getElementById("char_msg").remove()
    }catch{}
    new_msg = document.createElement("div")
    new_msg.className = "char_say_bar"
    new_msg.id = "char_msg"
    var html = ""
    for(msg of msg_obj["msg"]){
        html+="<p>"+msg+"</p>"
    }
    new_msg.innerHTML=html
    msg_bar.appendChild(new_msg)
    char_img.src="char/"+cid+msg_obj["char_img"]
    out_char()
}
function out_char(){
    document.getElementById("char").className="char_helper out_char_a"
}
function back_char(){
    document.getElementById("char").className="char_helper back_char_a"
}
async function play_audio(audio_file){
    var audio = new Audio(audio_file) 
    audio.play()
}
async function play_chat(c_name){
    try{
        var chat_data = await window.pywebview.api.get_charData()
        o_chat_data = chat_data
        chat_data=chat_data["wpp_data"]["chat"][c_name]
        play_audio("char/"+o_chat_data["wpp_data"]["char_id"]+"/"+ chat_data["voice"])
        for(c of chat_data["content"]){
            setTimeout(change_msg,c["time"],o_chat_data["wpp_data"]["char_id"],c["msg"])
        }
        setTimeout(back_char,chat_data["voice_time"])
        back_char()
    }catch(e){
        console.error(e)
    }
}
function load_file(file,type){
    var link = document.createElement('link');
    link.setAttribute('rel', type);
    link.setAttribute('href', file);
    document.body.appendChild(link);
}
async function load_plug_css(){
    load_file('./resources/char_plug.css','stylesheet')
    load_file('./char/'+await window.pywebview.api.get_charId()+'/plug.css','stylesheet')
}
setTimeout(load_plug_css,300)